package com.aw.baselib.widgets.font;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.text.InputFilter;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.EditText;

import com.aw.baselib.R;
import com.aw.baselib.base.AwBaseApplication;
import com.aw.baselib.util.AwDisplayUtil;
import com.aw.baselib.util.AwToastUtil;

/**
 * @author : hzw
 * https://blog.csdn.net/yuanxw44/article/details/80019501
 */
@SuppressLint("AppCompatCustomView")
public abstract class AwEtRegularBase extends EditText {

    private Context mContext;
    private int defaultLength = 20;
    private boolean isSingleLine = true;
    protected abstract void setViewBg();

    public AwEtRegularBase(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AwEtAttrs);
        defaultLength = a.getInteger(R.styleable.AwEtAttrs_maxInputLength, 20);
        isSingleLine = a.getBoolean(R.styleable.AwEtAttrs_isSingleLine, true);
        setHintTextColor(getResources().getColor(R.color.color_app_gray_light));
        setTextColor(getResources().getColor(R.color.color_app_blue_night));
        setTextSize(14);
        if(isSingleLine) {
            setSingleLine();
            setEllipsize(TextUtils.TruncateAt.END);
//            setHeight(AwDisplayUtil.dip2px(context, 45));
        } else {
//            setMinHeight(AwDisplayUtil.dip2px(context, 45));
            setPadding(AwDisplayUtil.dip2px(context, 14), AwDisplayUtil.dip2px(context, 5), AwDisplayUtil.dip2px(context, 14), AwDisplayUtil.dip2px(context, 5));
        }
        setFilters(new InputFilter[] { new InputFilter.LengthFilter(defaultLength) });
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    public int getMaxLines() {
        return 1;
    }

    @Override
    public boolean isSingleLine() {
        return true;
    }

    public void showEmptyEmailHint() {
            showCustomHint("请输入邮箱");
    }

    public void showEmptyPwdHint() {
        showCustomHint("请输入密码");
    }

    public void showEmptyPhoneHint() {
        showCustomHint("请输入手机号");
    }

    public void showEmptyCommonHint() {
        showCustomHint("请输入内容");
    }

    public void showFormatHint() {
        showCustomHint("异常! 格式错误");
    }

    public void showFormatPwdHint() {
//        showCustomHint("Please enter 6-20 characters, using uppercase and lowercase letters and numbers.");
        showCustomHint("异常! 格式错误");
    }

    public void showInvalidCodeHint() {
        showCustomHint("验证码无效，请重新输入");
    }

    public void showDifferentPwdHint() {
        showCustomHint("两次密码不一致，请重新输入");
    }

    public void showSamePwdHint() {
        showCustomHint("新密码不能与旧密码相同");
    }

    public void showDifferentEmailHint() {
        showCustomHint("邮箱验证不一致");
    }

    public void showCustomHint(String msg) {
        setViewBg();
        AwToastUtil.showShort(AwBaseApplication.getInstance(), msg);
    }

}
